import {get, post} from "../../utils/req";
import {alert} from "../../components/common/alert";

const empty = {total: 0, list: [], page: 1, size: 10};

const emptyOccupation = {date: [], maxLessonNumber: 0};

export async function summary() {
    try {
        return await get('/private/lesson_schedule/summary') || [];
    } catch (e) {
        alert(`获取课程编排失败：${e}`, 'error');
        return [];
    }
}

export async function schedules(classGroupId, thisWeek) {
    try {
        return await get(`/private/lesson_schedule/list?classGroupId=${classGroupId}&thisWeek=${thisWeek}`) || [];
    } catch (e) {
        alert(`获取课程编排失败：${e}`, 'error');
        return [];
    }
}

export async function save(data) {
    try {
        await post('/private/lesson_schedule/save', data);
        return true;
    } catch (e) {
        alert(`保存课程编排失败：${e}`, 'error');
        return false;
    }
}

export async function occupied(classGroupId, thisWeek) {
    try {
        return await get(`/private/lesson_schedule/occupied?classGroupId=${classGroupId}&thisWeek=${thisWeek}`) ||
            emptyOccupation;
    } catch (e) {
        alert(`获取已占用时间失败：${e}`, 'error');
        return emptyOccupation;
    }
}

export async function unconfirmed() {
    try {
        return await get('/private/lesson_schedule/unconfirmed') || [];
    } catch (e) {
        alert(`获取课程失败：${e}`, 'error');
        return [];
    }
}

export async function confirm(data) {
    try {
        await post('/private/lesson_schedule/confirm', data);
        return true;
    } catch (e) {
        alert(`销课失败：${e}`, 'error');
        return false;
    }
}

export async function history(data) {
    try {
        return await post('/private/lesson_schedule/history', data) || empty;
    } catch (e) {
        alert(`获取授课历史失败：${e}`, 'error');
        return empty;
    }
}

export async function scheduleList(data) {
    try {
        return await post('/private/lesson_schedule/schedules', data) || [];
    } catch (e) {
        alert(`获取课程表失败：${e}`, 'error');
        return [];
    }
}

export async function recent() {
    try {
        return await get('/private/lesson_schedule/recent') || [];
    } catch (e) {
        alert(`获取课程表失败：${e}`, 'error');
        return [];
    }
}

export async function unconfirmedCount() {
    try {
        return await get('/private/lesson_schedule/unconfirmed_count') || 0;
    } catch (e) {
        alert(`获取未确认课程数失败：${e}`, 'error');
        return 0;
    }
}

export async function scheduleStatus() {
    try {
        return await get('/private/lesson_schedule/schedule_status') || [];
    } catch (e) {
        alert(`获取课程编排失败：${e}`, 'error');
        return [];
    }
}
